<?php 
// array JSON response
$response = array();
 
// db connect class
require_once __DIR__ . '/connect.php';
 
// conectar com db
$db = new DB_CONNECT();

mysql_query("SET NAMES 'utf8'");
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_results=utf8');

function consultaPet() {
		$cnpj = $_GET['cnpj'];
	 
		// get a product from tbpetshop
		$result = mysql_query("SELECT nome, endereco, numero_end, bairro, uf, cep, email, telefone, ddd, horario_atendimento, horario_atendimento2, expediente, expediente2 FROM tbpetshop WHERE cnpj = '$cnpj'");
	 
		if (!empty($result)) {
			// checar resultados nulos
			if (mysql_num_rows($result) > 0) {
	 
				$result = mysql_fetch_array($result);
	 
				$petshop = array();
				$petshop["nome"] = $result["nome"];
				$petshop["endereco"] = $result["endereco"];
				$petshop["numero_end"] = $result["numero_end"];
				$petshop["bairro"] = $result["bairro"];
				$petshop["uf"] = $result["uf"];
				$petshop["cep"] = $result["cep"];
				$petshop["horario_atendimento"] = $result["horario_atendimento"];
				$petshop["horario_atendimento2"] = $result["horario_atendimento2"];
				$petshop["expediente"] = $result["expediente"];
				$petshop["expediente2"] = $result["expediente2"];
				$petshop["email"] = $result["email"];
				$petshop["telefone"] = $result["telefone"];
				$petshop["ddd"] = $result["ddd"];
				
				// success
				$response["success"] = 1;
	 
				// petshop node
				$response["petshop"] = array();
	 
				array_push($response["petshop"], $petshop);
	 
				// echo JSON response
				echo json_encode($response);
			} else {
				// Nenhum petshop encontrado
				$response["success"] = 0;
				$response["message"] = "Nenhum petshop encontrado";
	 
				echo json_encode($response);
			}
		} else {
			// Nenhum petshop encontrado
			$response["success"] = 0;
			$response["message"] = "Nenhum petshop encontrado";
	 
			echo json_encode($response);
		}
}

function todosPet() {
	// get all petshops from tbpetshop
	$result = mysql_query("SELECT cnpj, nome, endereco, expediente, expediente2, horario_atendimento, horario_atendimento2, cep FROM tbpetshop") or die(mysql_error());
	
     if (!empty($result)) {
	 
		// checar resultados nulos
		if (mysql_num_rows($result) > 0) {
    
			// petshops node
			$response["petshops"] = array();
 
			while ($row = mysql_fetch_array($result)) {
	
				// temp cachorro array
				$petshop = array();        
				$petshop["cnpj"] = $row["cnpj"];
				$petshop["nome"] = $row["nome"];
				$petshop["endereco"] = $row["endereco"];
				$petshop["expediente"] = $row["expediente"];
				$petshop["expediente2"] = $row["expediente2"];
				$petshop["horario_atendimento"] = $row["horario_atendimento"];
				$petshop["horario_atendimento2"] = $row["horario_atendimento2"];
				$petshop["cep"] = $row["cep"];
		
				// add unico cachorro no responde array
				array_push($response["petshops"], $petshop);
			}
		
			// success
			$response["success"] = 1;
 
			// echo JSON response
			echo json_encode($response);
		}
		else {
		    // nenhum cachorro encontrado
			$response["success"] = 0;
			$response["message"] = "Nenhum tbpetshop encontrado";
 
			// echo nenhum cachorro JSON
			echo json_encode($response);
		}
	}
}

if (strcmp($_GET['metodo'],"todosPet") == 0) { 
	todosPet();
}  
if (strcmp($_GET['metodo'],"consultaPet") == 0) { 
	consultaPet();
} 
?>